<template>
  <el-breadcrumb separator="/" class="mb-2 mt-1" style="vertical-align: bottom">
    <el-breadcrumb-item>
      <div class="flex align-center">
        <el-icon :size="18"> <DataAnalysis /> </el-icon
        ><span class="pl">報表管理</span>
      </div>
    </el-breadcrumb-item>
    <el-breadcrumb-item>销售報表</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card class="box-card" shadow="hover">
    <template #header>
      <div class="flex justify-between flex-wrap">
        <el-space wrap :size="10" class="mt">
          <el-select
            v-model="params.date_type"
            placeholder="类型"
            style="width: 100px"
            @change="typeChange"
          >
            <el-option
              v-for="item in dateType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <el-date-picker
            v-if="params.date_type != 'year'"
            v-model="myDate"
            :type="type_picker"
            range-separator="To"
            start-placeholder="開始日期"
            end-placeholder="結束日期"
            :editable="false"
            :disabled-date="disabledDateFun"
          />
          <el-input
            v-model="params.goods_name"
            style="width: 150px"
            placeholder="商品名称"
          />
          <el-button :icon="Search" plain @click="search">查詢</el-button>
        </el-space>
        <el-space wrap :size="10" class="mt">
          <el-button type="primary" plain>下载</el-button>
          <el-button type="primary" plain>图表</el-button>
        </el-space>
      </div>
    </template>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="120" />
      <el-table-column prop="goods_name" label="商品名称" />
      <el-table-column prop="goods_desc" label="商品描述" min-width="300" />
      <el-table-column prop="view_num" label="浏览量" />
      <el-table-column prop="sale_num" label="销售量" />
      <el-table-column prop="ship_num" label="出货数量" />
      <el-table-column prop="unship_num" label="未出货数量" />
      <el-table-column prop="cancel_num" label="取消数量" />
      <el-table-column label="操作" width="80">
        <template #default="scope">
        <el-space direction="vertical" :size="5">
          <el-button
            link
            size="small"
            type="primary"
            @click="handleEdit(scope.$index, scope.row)"
            >客户列表</el-button
          >
          <el-button
            link
            size="small"
            type="danger"
            @click="handleEdit(scope.$index, scope.row)"
            >销售来源</el-button
          >
           </el-space>
        </template>
      </el-table-column>
    </el-table>
    <div v-if="total > params.page_size" class="mt-2 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :page-sizes="[20, 40, 60]"
        :small="false"
        :disabled="false"
        :background="true"
        layout="total, sizes, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { Search, DataAnalysis } from "@element-plus/icons-vue";
import { $saleList } from "@api/report";
import { dayjs } from "element-plus";
let params = ref({
  current_page: 1,
  page_size: 10,
  goods_name: "",
  date_type: "date",
  start_time: "",
  end_time: "",
});
const type_picker = ref("daterange");
const loading = ref(false);
const total = ref(0);
const tableData = ref([]);
let myDate = ref([]);
const dateType = ref([
  { id: "date", name: "日" },
  { id: "month", name: "月" },
  { id: "year", name: "年" },
]);
onMounted(() => {
  getData();
});
const getData = async () => {
  if (myDate.value.length != 0) {
    params.value.start_time = dayjs(myDate.value[0]).format("YYYY-MM-DD");
    params.value.end_time = dayjs(myDate.value[1]).format("YYYY-MM-DD");
  }
  let res = await $saleList(params.value);
  if (res.code == 0) {
    tableData.value = res.data.table;
    total.value = res.data.total;
  }
};
const handleSizeChange = (val: number) => {
  params.value.page_size = val;
  getData();
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  params.value.current_page = val;
  getData();
  console.log(`current page: ${val}`);
};
const search = () => {
  console.log(params.value);
  getData();
};
const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(params);
  getData();
};
const typeChange = (val) => {
  if (val == "date") {
    type_picker.value = "daterange";
  } else {
    type_picker.value = "monthrange";
  }
  myDate.value = [];
};
const disabledDateFun = (date) => {
  let now = dayjs().subtract(1, "day");
  if (date < now) return false;
  return true;
};
</script>

<style lang="scss" scoped></style>
